/*******************************************************************************
 * Copyright (c) 2011 Red Hat, Inc.
 *  All rights reserved.
 * This program is made available under the terms of the
 * Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Red Hat, Inc. - initial API and implementation
 *
 * @author Bob Brodt
 ******************************************************************************/
package org.camunda.bpm.modeler.ui.preferences;

import org.camunda.bpm.modeler.core.Activator;
import org.camunda.bpm.modeler.core.preferences.Bpmn2Preferences;
import org.camunda.bpm.modeler.ui.Messages;
import org.eclipse.core.resources.IProject;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.dialogs.PropertyPage;
import org.osgi.service.prefs.BackingStoreException;

public class Bpmn2PropertyPage extends PropertyPage {

	private Bpmn2Preferences prefs;
	
	private Button btnShowAdvancedProperties;
	private Button btnShowDescriptions;
	private Button btnCheckProjectNature;
	
	private BPMNDIAttributeDefaultCombo cboIsHorizontal;
	private BPMNDIAttributeDefaultCombo cboIsExpanded;
	private BPMNDIAttributeDefaultCombo cboIsMessageVisible;
	private BPMNDIAttributeDefaultCombo cboIsMarkerVisible;
	
	private Button btnGeneratePng;
	
	public Bpmn2PropertyPage() {
		super();
		setTitle("BPMN2");
		setDescription(Messages.Bpmn2PreferencePage_HomePage_Description);
	}

	@Override
	protected Control createContents(Composite parent) {
		loadPrefs();
		
		Composite container = new Composite(parent, SWT.NULL);
		container.setLayout(new GridLayout(3, false));

		Label label = new Label(container, SWT.NONE);
		label.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1));
		label.setText(Bpmn2Preferences.PREF_TARGET_RUNTIME_LABEL);
		
		btnShowAdvancedProperties = new Button(container, SWT.CHECK);
		btnShowAdvancedProperties.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 3, 1));
		btnShowAdvancedProperties.setText(Bpmn2Preferences.PREF_SHOW_ADVANCED_PROPERTIES_LABEL);
		
		btnShowDescriptions = new Button(container, SWT.CHECK);
		btnShowDescriptions.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 3, 1));
		btnShowDescriptions.setText(Bpmn2Preferences.PREF_SHOW_DESCRIPTIONS_LABEL);

		btnCheckProjectNature = new Button(container, SWT.CHECK);
		btnCheckProjectNature.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 3, 1));
		btnCheckProjectNature.setText(Bpmn2Preferences.PREF_CHECK_PROJECT_NATURE_LABEL);

		// Default values for optional BPMN DI attributes
		Group group = new Group(container, SWT.BORDER);
		group.setLayout(new GridLayout(3,false));
		group.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 3, 1));
		group.setText("Default values for BPMN Diagram Interchange (DI) optional attributes");

		cboIsHorizontal = new BPMNDIAttributeDefaultCombo(group);
		cboIsHorizontal.setText(Bpmn2Preferences.PREF_IS_HORIZONTAL_LABEL);
		
		cboIsExpanded = new BPMNDIAttributeDefaultCombo(group);
		cboIsExpanded.setText(Bpmn2Preferences.PREF_IS_EXPANDED_LABEL);
		
		cboIsMessageVisible = new BPMNDIAttributeDefaultCombo(group);
		cboIsMessageVisible.setText(Bpmn2Preferences.PREF_IS_MESSAGE_VISIBLE_LABEL);
		
		cboIsMarkerVisible = new BPMNDIAttributeDefaultCombo(group);
		cboIsMarkerVisible.setText(Bpmn2Preferences.PREF_IS_MARKER_VISIBLE_LABEL);
		
		createDiagramExportComposite(container);

		initData();

		return container;
	}

	private Composite createDiagramExportComposite(Composite parent) {
		Composite composite = new Composite(parent, SWT.NO_SCROLL);
		GridLayout layout = new GridLayout();
		layout.marginWidth = 0;
		layout.marginHeight = 0;

		composite.setLayout(layout);
		composite.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 2, 1));

		btnGeneratePng = new Button(composite, SWT.CHECK);
		btnGeneratePng.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, false, 2, 1));
		btnGeneratePng.setText(Bpmn2Preferences.PREF_TOGGLE_DIAGRAM_GENERATION_LABEL);
		
		return composite;
	}

	private void restoreDefaults() {
		prefs.restoreDefaults(true);
		prefs.getRuntime();
		initData();
	}

	@Override
	protected void performDefaults() {
		super.performDefaults();
		restoreDefaults();
	}
	
	public void loadPrefs() {
		IProject project = (IProject) getElement().getAdapter(IProject.class);
		prefs = Bpmn2Preferences.getInstance(project);
		prefs.load();
	}

	private void initData() {
		btnShowAdvancedProperties.setSelection( prefs.getShowAdvancedPropertiesTab() );
		btnShowDescriptions.setSelection( prefs.getShowDescriptions() );
		btnCheckProjectNature.setSelection( prefs.getCheckProjectNature() );
		cboIsHorizontal.setValue(prefs.getIsHorizontal());
		cboIsExpanded.setValue(prefs.getIsExpanded());
		cboIsMessageVisible.setValue( prefs.getIsMessageVisible());
		cboIsMarkerVisible.setValue(prefs.getIsMarkerVisible());
		
		btnGeneratePng.setSelection(prefs.getBoolean(Bpmn2Preferences.PREF_TOGGLE_DIAGRAM_GENERATION, true));
	}

	@Override
	public boolean performOk() {
		setErrorMessage(null);
		try {
			updateData();
		} catch (BackingStoreException e) {
			Activator.showErrorWithLogging(e);
		}
		return true;
	}

	@Override
	public void dispose() {
		prefs.dispose();
		super.dispose();
	}

	private void updateData() throws BackingStoreException {
		prefs.setShowAdvancedPropertiesTab(btnShowAdvancedProperties.getSelection());
		prefs.setShowDescriptions(btnShowDescriptions.getSelection());
		prefs.setCheckProjectNature(btnCheckProjectNature.getSelection());
		prefs.setIsHorizontal(cboIsHorizontal.getValue());
		prefs.setIsExpanded(cboIsExpanded.getValue());
		prefs.setIsMessageVisible(cboIsMessageVisible.getValue());
		prefs.setIsMarkerVisible(cboIsMarkerVisible.getValue());
		
		prefs.setBoolean(Bpmn2Preferences.PREF_TOGGLE_DIAGRAM_GENERATION, btnGeneratePng.getSelection());
		prefs.save();
	}
}
